<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_sweepstake\Entity\ShusySweepstake;
use Drupal\shusy_sweepstake\Entity\ShusySweepstakeItem;

/**
 * @OA\Schema(
 *   schema="SweepstakeConfig",
 *   type="object",
 *   description="SweepstakeConfig"
 * )
 */
class SweepstakeConfig extends AbstractBaseModel {

  protected function getEntityType() {
    return 'shusy_sweepstake';
  }

  protected function setValue() {
    if ($this->entity instanceof ShusySweepstake) {
      $value = [
        /**
         * @OA\Property(
         *   property="id",
         *   type="integer",
         *   description="ID",
         * ),
         */
        'id' => (int) $this->entity->id,
        /**
         * @OA\Property(
         *   property="list",
         *   type="array",
         *   description="奖项列表",
         *   @OA\Items(ref="#/components/schemas/SweepstakeItem")
         * ),
         */
        'list' => $this->getList(),
        /**
         * @OA\Property(
         *   property="content",
         *   type="string",
         *   description="抽奖说明",
         * ),
         */
        'content' => (string) $this->entity->content,
        //$validity_start = 0;
        //$validity_end = 0;
        /**
         * @OA\Property(
         *   property="validityStart",
         *   type="string",
         *   description="抽奖开始时间",
         * ),
         */
        'validityStart' => (string) $this->formatDate($this->entity->validity_start, 'Y-m-d H:i:s'),
        /**
         * @OA\Property(
         *   property="validityEnd",
         *   type="string",
         *   description="抽奖结束时间",
         * ),
         */
        'validityEnd' => (string) $this->formatDate($this->entity->validity_end, 'Y-m-d H:i:s'),
      ];
      $this->merge($value);
    }
  }

  protected function getList() {
    $list = [];
    $es = shusy_sweepstake_item_load_multiple(FALSE, [
      'sweepstake_id' => $this->entity->id,
      'status' => 1,
    ]);
    foreach ($es as $e) {
      if ($e instanceof ShusySweepstakeItem) {
        $list[] = SweepstakeItem::get($e->id);
      }
    }
    return $list;
  }
}
